package com.wjerp.tplus.server.api.controller;

import com.wjerp.tplus.server.api.controller.base.BaseController;
import com.wjerp.tplus.server.api.request.AddReimburseMemberInfoRequest;
import com.wjerp.tplus.server.api.request.QueryReimburseMemberListRequest;
import com.wjerp.tplus.server.api.response.PageDataResponse;
import com.wjerp.tplus.server.api.response.ReimburseMemberInfoResponse;
import com.wjerp.tplus.server.common.constant.DataStatus;
import com.wjerp.tplus.server.common.exception.BusinessException;
import com.wjerp.tplus.server.common.exception.ServiceException;
import com.wjerp.tplus.server.common.model.BaseResponseData;
import com.wjerp.tplus.server.common.security.JwtUser;
import com.wjerp.tplus.server.domain.entity.invoice.mng.ReimburseMemberInfo;
import com.wjerp.tplus.server.domain.service.ReimburseMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lishuailei
 */
@Api(tags = "管理端--报销人员管理",description="管理端--报销人员管理相关api")
@RestController
@RequestMapping("reimburseMember")
public class ReimburseMemberManage extends BaseController {

    @Resource
    private ReimburseMemberService reimburseMemberService;

    @ApiOperation("报销人员信息查询（分页）")
    @PostMapping("loadReimburseMember")
    public ResponseEntity<BaseResponseData> loadReimburseMember(@RequestBody QueryReimburseMemberListRequest request){
        PageDataResponse<ReimburseMemberInfoResponse> pageDataResponse = reimburseMemberService.loadReimburseMember(request);
        return success(pageDataResponse);
    }


    @ApiOperation("获取所有报销人员信息列表(可用的)")
    @GetMapping("loadAllReimburseMember")
    public ResponseEntity<BaseResponseData> loadAllReimburseMember(){
        List<ReimburseMemberInfo> reimburseMemberInfoList = reimburseMemberService.loadAllReimburseMember();
        return success(reimburseMemberInfoList);
    }

    @ApiOperation("新增报销人员")
    @PostMapping("addReimburseMember")
    public ResponseEntity<BaseResponseData> addReimburseMember(@RequestBody AddReimburseMemberInfoRequest request){
        try {
            JwtUser currentLoginUserInfo = getCurrentLoginUserInfo();
            reimburseMemberService.addReimburseMember(request,currentLoginUserInfo.getDisplayUserId(),currentLoginUserInfo.getCompanyCode());
            return success(true);
        }catch (BusinessException ex) {
            return fail(ex.getErrorCode(),ex.getErrorMsg(),ex);
        }
    }

    @ApiOperation("导入报销人员")
    @PostMapping("importReimburseByFile")
    public ResponseEntity<BaseResponseData> importReimburseByFile(
            @RequestParam("file") MultipartFile file) {
        try {
            JwtUser currentLoginUserInfo = getCurrentLoginUserInfo();
            reimburseMemberService.importReimburseByFile(file,currentLoginUserInfo.getCompanyCode(),currentLoginUserInfo.getDisplayUserId());
            return success(true);
        } catch (ServiceException e) {
            e.printStackTrace();
            return fail(e.getErrorCode(),e.getErrorMsg(),e);
        } catch (BusinessException e) {
            e.printStackTrace();
            return fail(e.getErrorCode(),e.getErrorMsg(),e);
        }
    }


    @ApiOperation("修改报销人员")
    @PostMapping("updateReimburseMember")
    public ResponseEntity<BaseResponseData> updateReimburseMember(@RequestBody AddReimburseMemberInfoRequest request){
        try {
            JwtUser currentLoginUserInfo = getCurrentLoginUserInfo();
            reimburseMemberService.updateReimburseMember(request,currentLoginUserInfo.getDisplayUserId(),currentLoginUserInfo.getCompanyCode());
            return success(true);
        }catch (BusinessException ex) {
            return fail(ex.getErrorCode(),ex.getErrorMsg(),ex);
        }
    }

    @ApiOperation("报销人员删除")
    @DeleteMapping("deleteReimburseMember/{reimburseMemberCode}")
    public ResponseEntity<BaseResponseData> deleteReimburseMember(
            @ApiParam(value="报销人员编码")
            @PathVariable(required = true)  String reimburseMemberCode
    ){
        try {
            JwtUser currentLoginUserInfo = getCurrentLoginUserInfo();
            reimburseMemberService.updateReimburseMemberStatus(reimburseMemberCode,
                    currentLoginUserInfo.getDisplayUserId(),
                    currentLoginUserInfo.getCompanyCode(),
                    DataStatus.DELETE);
            return success(true);
        }catch (BusinessException e) {
            e.printStackTrace();
            return fail(e.getErrorCode(), e.getErrorMsg(), e.getCause());
        }
    }

    @ApiOperation("报销人员恢复")
    @PutMapping("recoveryReimburseMember/{reimburseMemberCode}")
    public ResponseEntity<BaseResponseData> recoveryReimburseMember(
            @ApiParam(value="报销人员编码")
            @PathVariable(required = true)  String reimburseMemberCode
    ){
        try {
            JwtUser currentLoginUserInfo = getCurrentLoginUserInfo();
            reimburseMemberService.updateReimburseMemberStatus(reimburseMemberCode, currentLoginUserInfo.getDisplayUserId(), currentLoginUserInfo.getCompanyCode(), DataStatus.RECOVERY);
            return success(true);
        }catch (BusinessException e) {
            e.printStackTrace();
            return fail(e.getErrorCode(), e.getErrorMsg(), e.getCause());
        }
    }
}
